
package combat.dudes.server.classes;

import combat.dudes.protocol.interfaces.OnboardSensorSystemInt;
import combat.dudes.protocol.interfaces.OnboardSystemInt;
import combat.dudes.protocol.interfaces.OnboardWeaponSystemInt;
import combat.dudes.protocol.interfaces.VehicleInt;
import combat.dudes.protocol.types.OnboardSystemStatus;
import combat.dudes.protocol.types.OnboardSystemType;
import combat.dudes.protocol.types.SensorSystemType;
import combat.dudes.protocol.types.WeaponSystemType;


/**
 * @author Jesper Keijzer
 */
public class VehicleUpdate {

	/**
	 * @param Vehicle
	 */
	public static void UpdateVehicle(VehicleInt vehicle) {
		if(vehicle.getHealth() < 90.0)
			destroySystems(vehicle, WeaponSystemType.TORPEDOLAUNCHER);
		if(vehicle.getHealth() < 50.0)
			destroySystems(vehicle, WeaponSystemType.GOALKEEPER);
		if(vehicle.getHealth() < 40.0) {
			destroySystems(vehicle, WeaponSystemType.GUIDED_MISSILELAUNCHER);
			destroySystems(vehicle, WeaponSystemType.UNGUIDED_MISSILELAUNCHER);
		}
		if(vehicle.getHealth() < 20.0)
			for(SensorSystemType sensorSystemType : SensorSystemType.values())
				destroySystems(vehicle, sensorSystemType);
	}
	public static void destroySystems(VehicleInt vehicle, WeaponSystemType type) {
		for(OnboardSystemInt onboardSystem : vehicle.getOnboardSystems())
			if(onboardSystem.getOnboardSystemType() == OnboardSystemType.WEAPON)
				if(((OnboardWeaponSystemInt) onboardSystem).getWeaponSystemType() == type)
					((OnboardWeaponSystemInt) onboardSystem).setOnboardSystemStatus(OnboardSystemStatus.DESTROYED);
	}
	public static void destroySystems(VehicleInt vehicle, SensorSystemType type) {
		for(OnboardSystemInt onboardSystem : vehicle.getOnboardSystems())
			if(onboardSystem.getOnboardSystemType() == OnboardSystemType.SENSOR)
				if(((OnboardSensorSystemInt) onboardSystem).getSensorSystemType() == type)
					((OnboardSensorSystemInt) onboardSystem).setOnboardSystemStatus(OnboardSystemStatus.DESTROYED);
	}
}
